#ifndef TEMPLATE_H_
#define TEMPLATE_H_

#include <string>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include "UTH.h"
#include "Hash.h"
#include "TemplateHooks.h"
#include <boost/thread/mutex.hpp>

using namespace std;
using namespace phnq::uth;

namespace phnq
{
	namespace phnqlets
	{
		class Tag;
		class Phnqlet;
	
		struct script_pool_item
		{
			UTHScript *script;
			set<string> depPaths;
			uint32_t depHash;
		};
	
		class Template
		{
			public:
				Template(string path, string type, Phnqlet *phnqlet);
				~Template();
				string getPath();
				int getIndex();
				string getType();
				string process();
				string processScript();
				string processStyle();
				void insertObjectCodeAndDependencies(stringbuf &sb, set<Phnqlet*> &phnqlets, set<Tag*> &tags, set<int> *exceptions=NULL);
				static Template* getTemplate(int idx);
			
			private:
				string path;
				Phnqlet *phnqlet;
				time_t initTime;
				int index;
				string type;
				TemplateHooks hooks;
			    boost::mutex scriptPoolMutex;
				map<string,stack<script_pool_item>* > scriptPools;
				uint32_t depHash;
				time_t depHashCalcTime;
			
				uint32_t calculateDepHash(set<string> depPaths);
				void addClientSideFramework(stringbuf &sb, set<string> &depPaths);
				script_pool_item getScriptFromPool();
				void returnScriptToPool(script_pool_item item);
		};
	}
}

#endif // TEMPLATE_H_
